/*
 * This class is starter code for project 2 - CS 202 Fall 2009.  There are
 * no methods for you to implement in this class but you will need to
 * re-implement this class for your generics-based tree.
 * 
 * This class represents a node inside a tree.  It holds a primitive integer
 * and pointers to two more nodes representing a left subtree and a right
 * subtree.
 * 
 * 
 * This class is based upon the TreeNode class in chapter 17 of Java: How
 * to Program 7th ed. (Deitel & Deitel).
 */

package project2;

/**
 *
 * @author Michael R. Peterson, based upon Deitel & Deitel 
 */
    // helper class to represent a node only containing ints
    public class TreeNode
    {
        // note package access: members can be accessed directly by classes
        // in the same package

        //Added Reference to parent not, it can be removed later.
        TreeNode parentNode;
        TreeNode leftNode;
        int data;
        TreeNode rightNode;
        
        // create a new node
        /**
         * Create a new node, set left and right pointers to null.
         * @param nodeData An integer to store in this node.
         */
        public TreeNode(int nodeData, TreeNode parentN)
        {
            data = nodeData;
            leftNode = rightNode = null;
            parentNode = parentN;
        }
        
        // locate insertion point and insert new node; ignore duplicate
        // values
        /**
         * Insert a new integer at the appropriate point below this node.  If 
         * the value is already in the tree, we will ignore duplicates.
         * @param insertValue the value to insert into the tree.
         */
        public void insert(int insertValue, TreeNode parentN)
        {
            // insert in left subtree
            if(insertValue < data)
            {
                // insert new TreeNode
                if(leftNode == null)
                {
                    parentNode = parentN;
                    leftNode = new TreeNode(insertValue, this);
                }
                else // continue traversing left subtree via recursion
                {
                    leftNode.insert(insertValue, this);
                }
            }
            else if(insertValue > data)  // insert in right
            {
                if(rightNode ==  null)
                {
                    parentNode = parentN;
                    rightNode = new TreeNode(insertValue, this);
                }
                else // continue traversing right subtree via recursion
                {
                    rightNode.insert(insertValue, this);
                }
            }
        } // end method insert
    }  // end class TreeNode
